/**
 * 复活赛API
 *
 * @author bian
 * @createDate 2016.7.30
 */

var db = require("../models/index");
var msgWrapper = require("../helpers/msgWrapper");
var DataAdaptor = require("../modelAdaptor/Data");
var task = require("../task/index");

var Data = db.models.Data;

module.exports = (router) => {

    /**
     * 得到空调，插线板，人体感应的状态
     *
     * @example /state?equipment=intelAir&type=wind
     */
    router.get("/state",function (req,res) {
        req.checkQuery("equipment").in(["intelAir","intelPatch","intelBody"]);  // 设备种类
        req.checkQuery("type").in(["wind","temperature","state"]);      // 数据种类
        var errors = req.validationErrors();
        if (errors) {
            res.send(msgWrapper(errors, true));
            return;
        }
        var equipment = req.query.equipment;
        var type = req.query.type;

        DataAdaptor
            .getState(equipment,type)
            .then(function(data){
                res.send(msgWrapper(data));
            });
    });

    /**
     * 得到门铃，传感器的数据
     */
    router.get("/data",function (req,res){
        req.checkQuery("equipment").in(["intelSensor","intelDoorBell"]);   // 设备种类
        req.checkQuery("type").in(["temperature","humidity","json"]);  // 数据种类
        req.checkQuery("limit");  // 限制
        req.checkQuery("time");  // Unix 时间戳，默认应该是当前时间
        req.checkQuery("duration"); // 多长时间内的数据
        var errors = req.validationErrors();
        if (errors) {
            res.send(msgWrapper(errors, true));
            return;
        }
        var equipment = req.query.equipment;
        var type = req.query.type;
        var limit = req.query.limit;
        var time = req.query.time;
        var duration = req.query.duration;
        
        DataAdaptor
            .getData(equipment,type,limit,time,duration)
            .then(function (data) {
                res.send(msgWrapper(data));
            });
    });

    /**
     * 设置设备的状态
     *
     * 设置插线板 ： 001
     * 设置空调 ： type + state
     */
    router.post("/setState",function (req,res){
        req.checkBody("equipment").in(["intelAir","intelPatch"]);
        req.checkBody("type").in(["state","wind","temperature"]);
        req.checkBody("value").notEmpty();
        var errors = req.validationErrors();
        if (errors) {
            res.send(msgWrapper(errors, true));
            return;
        }
        var equipment = req.body.equipment;
        var type = req.body.type;
        var value = req.body.value;

        var sockets = req.app.locals.sockets;
        for(var i in sockets){      // 单一设备所有客户端全部接受
            sockets[0].emit(equipment,{type:type,value : value});
            sockets[0].broadcast.emit(equipment,{type:type,value : value});
        }

        DataAdaptor
            .setState(equipment,type,value)
            .then(function(){
                res.send(msgWrapper("ok"));
            });
    });

    /**
     * get all plans
     */
    router.get("/plan/collection",function(req,res){
        DataAdaptor
            .getPlans()
            .then(function(plans){
                res.send(plans.map((pl) => {
                    return {
                        equipment : pl.equipment,
                        type : pl.type,
                        value : pl.value
                    };
                }));
            });
    });
    
    /**
     * set the plan of intel
     */
    router.post("/plan",function(req,res) {
        req.checkBody("equipment").in(["intelBody"]);
        req.checkBody("value").notEmpty();
        var errors = req.validationErrors();
        if (errors) {
            res.send(msgWrapper(errors, true));
            return;
        }
        var equipment = req.body.equipment;
        var value = req.body.value;

        DataAdaptor
            .setPlan(equipment,JSON.stringify(value))
            .then(function(){
                res.send(msgWrapper("ok"));
            });
    });
};